package cn.wsgfz;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractTestCase {

	private String name = null;

	public static final Object aNullObject = new Object();

	public static final byte aByte = (byte) 255;

	public static final char aChar = 'a';

	public static final char aChineseChar = '中';

	public static final short aBufferedShort = 0;

	public static final short aUnBufferedShort = (short) 65535;

	public static final int aBufferedInt = 0;

	public static final int aUnBufferedInt = 100000;

	public static final long aLong = 100000000L;

	public static final float aFloat = 5000.0f;

	public static final double aDouble = 109.21d;

	public static final String aNullString = null;

	public static final String aConstantString = "0123456789";

	public static final String aNewConstructString = new String("9876543210");

	public static final Object[] aNullObjectArray = new Object[0];

	public static final Object[] aOneObjectArray = new Object[1];

	public static final Object[] aTwoObjectArray = new Object[2];

	public static final Object[] aThreeObjectArray = new Object[3];

	public static final Object[][] aNullTwoDimensionObjectArray = new Object[0][0];

	public static final byte[][] aBigByteArray = new byte[8 * 1024][8 * 1024];

	// result Map
	private static final Map<String, Long> result = new HashMap<String, Long>(0);
	private static final Map<String, Object> data = new HashMap<String, Object>(
			0);

	public static final String resultString = "the 【%s】 object use 【%d】 bytes memory size ";

	static {

		data.put("aNullObject", aNullObject);
		data.put("aByte", aByte);
		data.put("aChar", aChar);
		data.put("aChineseChar", aChineseChar);
		data.put("aBufferedShort", aBufferedShort);
		data.put("aUnBufferedShort", aUnBufferedShort);
		data.put("aBufferedInt", aBufferedInt);
		data.put("aUnBufferedInt", aUnBufferedInt);
		data.put("aLong", aLong);
		data.put("aFloat", aFloat);
		data.put("aDouble", aDouble);

		data.put("aNullString", aNullString);
		data.put("aConstantString", aConstantString);
		data.put("aNewConstructString", aNewConstructString);
		data.put("aNullObjectArray", aNullObjectArray);
		data.put("aOneObjectArray", aOneObjectArray);
		data.put("aTwoObjectArray", aTwoObjectArray);
		data.put("aThreeObjectArray", aThreeObjectArray);
		data.put("aNullTwoDimensionObjectArray", aNullTwoDimensionObjectArray);

		data.put("aBigByteArray", aBigByteArray);

	}

	public AbstractTestCase(String name) {
		this.name = name;
	}

	// 实现策略
	public abstract long countObjectSize(Object obj);

	// 测试用例
	public void countTheResult() {
		for (String testCaseName : data.keySet()) {
			Object obj = data.get(testCaseName);
			Long count = this.countObjectSize(obj);
			result.put(testCaseName, count);
		}
	}

	// 打印结果
	public void printTheResult() {
		System.out.println("the " + name
				+ " count method run start.........................");
		for (String testCaseName : result.keySet()) {
			System.out.println(String.format(resultString, testCaseName,
					result.get(testCaseName)));
		}
		System.out.println("the " + name
				+ " count method run finished .........................");
		System.out.println("~~  :)~");
	}
}
